package pl.plesken.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import pl.plesken.languages.SetLanguage;

public class AddNewHost extends Dialog {

	private Text nameText;
	protected Object result;
	protected Shell shell;
	String name;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public AddNewHost(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public AddNewHost(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setLayout(new GridLayout());
		shell.setSize(362, 112);
		shell.setText(SetLanguage.getString("AddNewHost.add")); //$NON-NLS-1$

		final Group group = new Group(shell, SWT.NONE);
		group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		group.setLayout(gridLayout);

		final Label nameLabel = new Label(group, SWT.NONE);
		final GridData gd_nameLabel = new GridData(SWT.FILL, SWT.CENTER, false, false);
		nameLabel.setLayoutData(gd_nameLabel);
		nameLabel.setText(SetLanguage.getString("AddNewHost.name")); //$NON-NLS-1$

		nameText = new Text(group, SWT.BORDER);
		final GridData gd_nameText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		nameText.setLayoutData(gd_nameText);

		final Button setButton = new Button(shell, SWT.NONE);
		setButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				name = nameText.getText();
				shell.close();
			}
		});
		final GridData gd_setButton = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
		setButton.setLayoutData(gd_setButton);
		setButton.setText(SetLanguage.getString("AddNewHost.set")); //$NON-NLS-1$
		//
	}

}
